<?php
/*
 * Created on 27.12.2006
 */
m2import('phpinchains/util/ValidatorHelper.php');

class phpinchains_validators_DBValidator
{
    
    function duplicate (& $env, & $actionform, $fieldname, $validationParam)
    {
        $params = phpinchains_util_ValidatorHelper::parseValidationParam($validationParam);
        $errors = & $actionform->getRef('validationErrors');
        if (empty($params['field']))
            $params['field'] = $fieldname;
        if (strlen($actionform->get($fieldname)) == 0)
        {
            return;
        }
        else
        {
            $conn = phpinchains_util_db_DataSource::getInstance()->getConnection();
            $sth  = $conn->query("select count(*) from {$params['table']} where {$params['field']} = ".
                $conn->quote($actionform->get($fieldname)).($params['cond']?' and '.$params['cond']:'').";");
            if (is_object($sth))
            {
                $res = $sth->fetch_row();
                if ($res[0] > 0) {
                    $errors->addField($fieldname);
                    return true;
                }
            }
            else {
                $errors->addField($fieldname);
                return true;
            }
        }
    }    
    
}

?>
